﻿using System;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfCustomShopEditer : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                ShowData();
            }
        }


        // public DataRow drTenant = null;

        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }

        private void ShowData()
        {
            string strId = msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0];
            if (string.IsNullOrEmpty(strId))
            {
                this.Response.Redirect("/");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo())
            {
                System.Data.DataSet ds = oCDbAccess.SelectById(strId);
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    this.Response.Redirect("/");
                    return;
                }
                else
                {
                    this.ExecJScript("$('#txtId').val('" + strId + "');");
                    DataRow drTenant = ds.Tables[0].Rows[0];
                    this.SetContrlValue("txtDEFAULT_HEADER_BG_COLOR", drTenant["DEFAULT_HEADER_BG_COLOR"].ToString());
                    if (drTenant["HEADER_SHOW_SHOP_NME"].ToString() == "True")
                    {
                        this.ExecJScript("$('#ckbHEADER_SHOW_SHOP_NME').attr('checked',true);");
                    }
                    else
                    {
                        this.ExecJScript("$('#ckbHEADER_SHOW_SHOP_NME').attr('checked',false);");
                    }

                    if (drTenant["DEFAULT_HEADER_IMG_URL"].ToString() != string.Empty)
                    {
                        this.ExecJScript("$('#head_img_selected').html('<img alt=\"\" src=\"" + drTenant["DEFAULT_HEADER_IMG_URL"].ToString() + "\" for=\"" + drTenant["DEFAULT_HEADER_IMG_URL"].ToString() + "\" width=\"100%\" height=\"100%\">');");
                    }
                    if (drTenant["LOGO_IMG_URL"].ToString() != string.Empty)
                    {
                        this.ExecJScript("$('#logo_img_selected').html('<img alt=\"\" src=\"" + drTenant["LOGO_IMG_URL"].ToString() + "\" for=\"" + drTenant["LOGO_IMG_URL"].ToString() + "\" width=\"100%\" height=\"100%\">');");
                    }
                    if (drTenant["HEADER_TEMPLATE_URL"].ToString() != string.Empty)
                    {
                        this.ExecJScript("$('#ckbHeadCust').attr('checked',true);");
                        this.ExecJScript("$('#divHeadTemplateEdit').show();");
                        this.ExecJScript("$('#divHeadTemplateEditdesc').show();"); 
                        this.ExecJScript("doBoundHeader('" + drTenant["HEADER_TEMPLATE_URL"].ToString() + "');");
                    }
                    else
                    {
                        this.ExecJScript("$('#ckbHeadCust').attr('checked',false);");  
                        this.ExecJScript("$('#divHeadTemplateEdit').hide();");
                        this.ExecJScript("$('#divHeadTemplateEditdesc').hide();"); 
                    }
                    if (drTenant["GOODS_LIST_TEMPLATE_URL"].ToString() != string.Empty)
                    {
                        this.ExecJScript("$('#ckbGoodsListCust').attr('checked',true);");
                        this.ExecJScript("$('#divContentTemplate').show();");
                        this.ExecJScript("$('#divContentTemplatedesc').show();"); 
                        this.ExecJScript("doBoundContent('" + drTenant["GOODS_LIST_TEMPLATE_URL"].ToString() + "');");
                    }
                    else
                    {
                        this.ExecJScript("$('#ckbGoodsListCust').attr('checked',false);");
                        this.ExecJScript("$('#divContentTemplate').hide();");
                        this.ExecJScript("$('#divContentTemplatedesc').hide();"); 
                    }

                    if (drTenant["GOODS_SEARCH_TEMPLATE_URL"].ToString() != string.Empty)
                    {
                        this.ExecJScript("$('#ckbSearchCust').attr('checked',true);");
                        this.ExecJScript("$('#divSearchTemplate').show();");
                        this.ExecJScript("$('#divSearchTemplatedesc').show();"); 
                        this.ExecJScript("doBoundSearch('" + drTenant["GOODS_SEARCH_TEMPLATE_URL"].ToString() + "');");
                    }
                    else
                    {
                        this.ExecJScript("$('#ckbSearchCust').attr('checked',false);");
                        this.ExecJScript("$('#divSearchTemplate').hide();");
                        this.ExecJScript("$('#divSearchTemplatedesc').hide();"); 
                    }

                    doShowHeadImgList(strId);
                }
            }
        }


        /// <summary>
        /// 商品规格
        /// </summary>
        private void doShowHeadImgList(string strId)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialHeadImg oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpCommercialHeadImg())
            { 

                DataSet ds = oCDbAccess.SelectByDealerId(strId);
                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript(@"var data = " + strjsontem1 + @";
                                    var tpl =HtmlUtil.htmlDecode(document.getElementById('head_img_temp').innerHTML);  
                                    laytpl(tpl).render(data, function(render){
                                        $('#content_top_img').empty().html(render);
                                    });
                ");
            }
        }

        private void doSaveSet()
        { 
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo())
            {
                System.Data.DataSet ds = oCDbAccess.SelectById(msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0]);
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    return;
                }
                DataRow drTenant = ds.Tables[0].Rows[0];

                drTenant["DEFAULT_HEADER_IMG_URL"] = this.GetPostHtValue("DEFAULT_HEADER_IMG_URL");
                drTenant["LOGO_IMG_URL"] = this.GetPostHtValue("LOGO_IMG_URL");
                drTenant["DEFAULT_HEADER_BG_COLOR"] = this.GetPostHtValue("DEFAULT_HEADER_BG_COLOR");
                drTenant["HEADER_SHOW_SHOP_NME"] = this.GetPostHtValue("HEADER_SHOW_SHOP_NME");



                string strHeadHtml = this.GetPostHtValue("HEADER_TEMPLATE_HTML");
                string strVpath = drTenant["HEADER_TEMPLATE_URL"].ToString();
                if (strHeadHtml != string.Empty)
                {
                    if (strVpath == string.Empty)
                    {
                        strVpath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase + msBsc.AppLogic.Cfg.SystemFileDir.FileDir.strSaveUploadSysFileDirV + GetRadomString() + ".html";
                        drTenant["HEADER_TEMPLATE_URL"] = strVpath;
                    }
                    string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpath);
                    System.IO.File.WriteAllText(strFFullPath, strHeadHtml, System.Text.Encoding.UTF8);
                }
                else
                {
                    drTenant["HEADER_TEMPLATE_URL"] = "";
                    if (strVpath != string.Empty)
                    {
                        if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(strVpath)))
                        {
                            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(strVpath));
                        }
                    }
                    ////直接生成文件，直接引用
                    //drTenant["HEADER_TEMPLATE_URL"] = "";
                }

                string strContendHtml = this.GetPostHtValue("GOODS_LIST_TEMPLATE_HTML");
                strVpath = drTenant["GOODS_LIST_TEMPLATE_URL"].ToString();

                if (strContendHtml!=string.Empty)
                {
                    if (strVpath == string.Empty)
                    {
                        strVpath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase + msBsc.AppLogic.Cfg.SystemFileDir.FileDir.strSaveUploadSysFileDirV + GetRadomString() + ".html";
                        drTenant["GOODS_LIST_TEMPLATE_URL"] = strVpath;
                    } 
                    System.IO.File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath(strVpath), strContendHtml, System.Text.Encoding.UTF8);
                }
                else
                {
                    //使用系统默认 
                    drTenant["GOODS_LIST_TEMPLATE_URL"] = "";
                    if (strVpath != string.Empty)
                    {
                        if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(strVpath)))
                        {
                            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(strVpath));
                        }
                    }
                }

                string strSearchHtml = this.GetPostHtValue("GOODS_SEARCH_TEMPLATE_HTML");
                strVpath = drTenant["GOODS_SEARCH_TEMPLATE_URL"].ToString();

                if (strSearchHtml != string.Empty)
                {
                    if (strVpath == string.Empty)
                    {
                        strVpath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.UrlBase + msBsc.AppLogic.Cfg.SystemFileDir.FileDir.strSaveUploadSysFileDirV + GetRadomString() + ".html";
                        drTenant["GOODS_SEARCH_TEMPLATE_URL"] = strVpath;
                    }
                    System.IO.File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath(strVpath), strSearchHtml, System.Text.Encoding.UTF8);
                }
                else
                {
                    //使用系统默认 
                    drTenant["GOODS_SEARCH_TEMPLATE_URL"] = "";
                    if (strVpath != string.Empty)
                    {
                        if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(strVpath)))
                        {
                            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(strVpath));
                        }
                    }
                }
                
                
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {


                    using (msBsc.AppLogic.DataAlc.CBizShpCommercialHeadImg oCBizShpCommercialHeadImg = new msBsc.AppLogic.DataAlc.CBizShpCommercialHeadImg())
                    {
                        DataSet dsItem = oCBizShpCommercialHeadImg.SelectByDealerId(drTenant["COMM_TENANT_ID"].ToString()); 

                        System.Collections.Generic.List<string> lsIds = new System.Collections.Generic.List<string>();
                        int iItemCount = int.Parse(GetPostHtValue("HEAD_IMG_COUNT"));
                        for (int i = 0; i < iItemCount; i++)
                        {
                            DataRow drNewItem;
                            if (dsItem.Tables[0].Rows.Count > i)
                            {
                                drNewItem = dsItem.Tables[0].Rows[i];
                            }
                            else
                            {
                                drNewItem = dsItem.Tables[0].NewRow();
                                drNewItem["HEAD_IMG_ID"] = oCBizShpCommercialHeadImg.DataObj.NewID();
                                drNewItem["COMM_TENANT_ID"] = drTenant["COMM_TENANT_ID"];
                                dsItem.Tables[0].Rows.Add(drNewItem);
                            }
                            drNewItem["SEQ_NUM"] = i;
                            drNewItem["IMG_URL"] = GetPostHtValue("IMG_URL_" + i.ToString());
                            drNewItem["BG_COLOR"] = GetPostHtValue("BG_COLOR_" + i.ToString());
                            drNewItem["LINK_URL"] = GetPostHtValue("LINK_URL_" + i.ToString());
                             

                        }

                        for (int i = dsItem.Tables[0].Rows.Count - 1; i >= iItemCount; i--)
                        {
                            dsItem.Tables[0].Rows[i].Delete();
                        }

                        oCBizShpCommercialHeadImg.Update(dsItem);

                    }
                    oCDbAccess.Update(ds);
                    ts.Complete();
                }
            }

            this.ExecJScript("layer.msg('成功保存店铺设置！',1,{type:1,shade:false});");
        }

        public override void DoAction()
        {
            if (this.strAction == "doSaveSet")
            {
                doSaveSet();
            }
        }
    }
}